<?php
/**
 * @copyright (C) .
 * @author Allan
 * @email
 * @date 2023.2.24
 *  默认主页
 */
namespace app\admin\controller;

use core\basic\Controller;

class BaseController extends Controller
{
    // 生成分类选择
    protected function makeKeySelect($tree, $selectid = null)
    {
        $list_html = '';
        foreach ($tree as $key => $value) {
            // 默认选择项
            if (!is_null($selectid) && $selectid == $key) {
                $select = "selected='selected'";
            } else {
                $select = '';
            }
            $list_html .= "<option value='{$key}' $select>{$value}</option>";
        }
        return $list_html;
    }

    protected function makeTreeSelect($tree, $selectid = null){
        $list_html = '';
        foreach ($tree as $value) {
            $id = $value['id'];
            $name = $value['name'];
            // 默认选择项
            if (!is_null($selectid) && $selectid == $id) {
                $select = "selected='selected'";
            } else {
                $select = '';
            }
            if( isset($value['child']) && !empty($value['child']) ){
                $list_html1 = '';
                foreach ($value['child'] as $value1){
                    if (!is_null($selectid) && $selectid == $value1['id']) {
                        $select1 = "selected='selected'";
                    } else {
                        $select1 = '';
                    }
                    $list_html1 .= "<option value='" . $value1['id'] ."' $select1>" . $value1['name'] . "</option>";
                }
                $list_html .= "<optgroup label='{$name}' $select>" . $list_html1 . "</optgroup >";

            }else{
                $list_html .= "<option value='{$id}' $select>{$name}</option>";
            }
        }
        return $list_html;
    }


}